package com.aduerr.v1shouyin.mvp.activity;

import android.content.Intent;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import com.aduerr.v1shouyin.R;
import com.aduerr.v1shouyin.common.Constants;
import com.aduerr.v1shouyin.entity.GetMemberListEntity;
import com.aduerr.v1shouyin.print.PrintObserver;
import com.aduerr.v1shouyin.print.PrinterWrapper;
import com.aduerr.v1shouyin.unioncard.BaseUnionActivity;
import com.aduerr.v1shouyin.util.SharedPreferencesUtils;
import com.aduerr.v1shouyin.util.ToastUtils;
import com.orhanobut.hawk.Hawk;
import io.reactivex.android.schedulers.AndroidSchedulers;

import java.math.BigDecimal;

public class MemberInchargListItemDetailActivity extends BaseUnionActivity {
    @BindView(R.id.img_back)
    ImageView imgBack;
    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.iv_icon_type)
    ImageView ivIconType;
    @BindView(R.id.tv_incargmoney)
    TextView tvIncargmoney;
    @BindView(R.id.tv_refoundmoney_type)
    TextView tvRefoundmoneyType;
    @BindView(R.id.tv_member_phone)
    TextView tvMemberPhone;
    @BindView(R.id.tv_congzhijine)
    TextView tvCongzhijine;
    @BindView(R.id.tv_zhengsongjine)
    TextView tvZhengsongjine;
    @BindView(R.id.tv_currentyue)
    TextView tvCurrentyue;
    @BindView(R.id.tv_zhifudangshi)
    TextView tvZhifudangshi;
    @BindView(R.id.tv_shouyinyuan)
    TextView tvShouyinyuan;
    @BindView(R.id.tv_mendian)
    TextView tvMendian;
    @BindView(R.id.tv_zhifushijian)
    TextView tvZhifushijian;
    @BindView(R.id.tv_print_note)
    TextView tvPrintNote;
    @BindView(R.id.tv_pay_state)
    TextView tvpaystate;
    @BindView(R.id.rl_6)
    RelativeLayout rlsendmoney;
    @BindView(R.id.rl_7)
    RelativeLayout rlCutMoney;
    @BindView(R.id.tv_order_id)
    TextView tvOrderId;
    @BindView(R.id.tv_cut_money)
    TextView tvCutMoney;

    private GetMemberListEntity.DataBean dataBeen;

    private String[] content = new String[20];


    @Override
    protected int getLayoutId() {
        return R.layout.activity_member_incharg_list_item_detail;
    }

    @Override
    protected void initBase() {
        super.initView();
        ButterKnife.bind(this);
        tvTitle.setText("订单详情");
        initdate();
    }

    private void initdate() {
        Intent intent = getIntent();
        if(intent != null) {
            dataBeen = (GetMemberListEntity.DataBean) intent.getSerializableExtra("date");
            content[1] = dataBeen.getOrderid();
            content[2] = dataBeen.getMoney().replace("-", "");
            content[3] = dataBeen.getSendmoney();
            String phone = dataBeen.getPhone();
            if(phone != null) {
                String maskNumber = phone.substring(0, 3) + "****" + phone.substring(7,
                        phone.length());
                content[4] = maskNumber;
            } else {
                content[4] = "";
            }
            content[5] = dataBeen.getHavemoney();
            content[6] = dataBeen.getName();
            content[7] = dataBeen.getShopname();
            content[8] = dataBeen.getCreatetime();
            content[9] = dataBeen.getSiteusername();
            content[13] = dataBeen.getHavemoney();
            content[11] = Hawk.get("company");
            content[12] = dataBeen.getIntegral() + "";
            String type = dataBeen.getType();
            if(type.equals("0")) {
                content[0] = "充值";
            } else {
                content[0] = "消费";
            }
//            content[10] = dataBeen.getOrdernote();//备注ordernote
            if(dataBeen.getType().equals("0")) {
                tvTitle.setText("充值详情");
                ivIconType.setImageResource(R.drawable.img_congzhi);
                tvRefoundmoneyType.setText("充值金额");
                rlsendmoney.setVisibility(View.VISIBLE);
                rlCutMoney.setVisibility(View.GONE);
                tvIncargmoney.setText(dataBeen.getMoney());
                tvCongzhijine.setText((new BigDecimal(dataBeen.getMoney()).subtract(new BigDecimal(dataBeen.getSendmoney())).toString()));
            } else {
                tvpaystate.setText("消费金额");
                rlsendmoney.setVisibility(View.GONE);
                rlCutMoney.setVisibility(View.GONE);
                tvTitle.setText("消费详情");
                ivIconType.setImageResource(R.drawable.img_xiaofei);
                tvRefoundmoneyType.setText("消费金额");
                Double aDouble = null;
                try {
                    aDouble = Double.valueOf(dataBeen.getMoney());
                    tvIncargmoney.setText(aDouble * (-1) + "");
                    tvCongzhijine.setText(aDouble * (-1) + "");
                } catch(NumberFormatException e) {
                    e.printStackTrace();
                    ToastUtils.showToast(MemberInchargListItemDetailActivity.this,
                            "金额转换异常");
                    tvIncargmoney.setText("更新中...");
                }
            }

            tvMemberPhone.setText(dataBeen.getPhone());
            tvZhengsongjine.setText(dataBeen.getSendmoney());
            tvCutMoney.setText(dataBeen.getSellerhongbaoamount());
            tvCurrentyue.setText(dataBeen.getHavemoney());
            tvZhifudangshi.setText("");
//            String sellername = Hawk.get("sellername", "");//收银员姓名
            tvShouyinyuan.setText(dataBeen.getSiteusername());//待确定
            tvMendian.setText(dataBeen.getShopname());
            tvZhifushijian.setText(dataBeen.getCreatetime());
            tvOrderId.setText(dataBeen.getOrderid());
        }

    }

    @OnClick({R.id.img_back, R.id.tv_print_note})
    public void onViewClicked(View view) {
        switch(view.getId()) {
            case R.id.img_back:
                finish();
                break;
            case R.id.tv_print_note:
                int printpage =
                        (int) SharedPreferencesUtils.getParam(MemberInchargListItemDetailActivity.this, "printpage", 1);
                if(printpage == 0) {
                    ToastUtils.showToast(MemberInchargListItemDetailActivity.this,
                            "打印张数设置为不打印,请先设置打印张数!!");
                    return;
                }
                print(content, Constants.PRINT_MEMBER_RECHARGE_RESULT, printpage);
                break;
        }
    }

    private void print(String[] content, int orderType, int printPage) {

        printOrder(content, orderType, printPage);

    }

    private void printOrder(String[] content, int orderType, int printPage) {
        PrinterWrapper.printPosOrder(null, content, orderType, printPage);
    }
}
